#!/usr/bin/env python
import sys
import datetime

from urlparse import urljoin

from xml.etree.ElementTree import Element, SubElement, ElementTree

from google.appengine.ext import webapp, db
from google.appengine.api import urlfetch

class Fetch(db.Model):
    ts = db.DateTimeProperty(auto_now_add=True)
    local_ip = db.StringProperty()
    remote_ip = db.StringProperty()
    headers = db.TextProperty()

class Dispatcher(webapp.RequestHandler):
    def get(self):        
        rss = Element("rss", { "version" : "2.0" })
        channel = SubElement(rss, "channel")
        
        self.addChildElements(channel, {
            "title" : "Sync douban with fanfou",
            "link" : "http://pingbus.appspot.com/",
            "description" : "Sync douban broadcast items to fanfou",
        
            "generator" : "pingbus",
            "generator" : datetime.datetime.now().strftime("%a, %d %b %Y %H:%M:%S"),
            "webMaster" : "flier.lu@gmail.com",            
        })
        
        
        f = Fetch(local_ip = urlfetch.fetch(urljoin(self.request.url, "/echo/")).content,
                  remote_ip = self.request.remote_addr,
                  headers = str(self.request.headers),
                  cookies = str(self.request.cookies))
        f.put()
        
        for f in Fetch.gql("ORDER BY ts DESC LIMIT 20"):            
            self.addChildElements(SubElement(channel, "item"), {
                "title" : "crawler from %s to %s" % (f.remote_ip, f.local_ip),
                "link" : urljoin(self.request.url, "/ip/%s" % f.remote_ip),
                "description" : str(f.headers), 
                "pubDate" : f.ts.strftime("%a, %d %b %Y %H:%M:%S")
            })

        self.response.headers['Content-Type'] = 'text/xml; charset=utf-8'
        ElementTree(rss).write(self.response.out, encoding="utf-8")
    
    def addChildElements(self, parent, children):
        for name, value in children.items():
            SubElement(parent, name).text = str(value)